package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.domain.ErpPurchaseRequestManagement;
import com.ruoyi.domain.PurchaseDetailBom;
import com.ruoyi.domain.PurchaseDetailBom2;
import com.ruoyi.mapper.ErpPurchaseRequestManagementMapper;
import com.ruoyi.mapper.PurchaseDetailBomMapper;
import com.ruoyi.mapper.PurchaseDetailBomMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service

public class PurchaseDetailBomImpl extends ServiceImpl<PurchaseDetailBomMapper, PurchaseDetailBom>  implements com.ruoyi.service.PurchaseDetailBomImpl {
@Autowired
    PurchaseDetailBomMapper purchaseDetailBomMapper;
    @Autowired
    ErpPurchaseRequestManagementMapper pur;
    @Autowired
    PurchaseDetailBomMapper2 purchaseDetailBomMapper2;


    @Override
    public boolean addmaterials(List<PurchaseDetailBom> ids, int key) {
        //将主键id加入到
        List<PurchaseDetailBom> collect = new ArrayList<>();
        boolean b = false;
//        List<PurchaseDetailBom> purchaseDetailBoms = new ArrayList<>();
//        for (Integer id : ids) {
//            PurchaseDetailBom item = new PurchaseDetailBom();
//            item.setMaterialId(id);
//            item .setRequisitionId(key);
        List<PurchaseDetailBom> purchaseDetailBoms = new ArrayList<>();
        for (PurchaseDetailBom id : ids) {
            PurchaseDetailBom item = new PurchaseDetailBom();
            item.setMaterialId(id.getId());
            item.setRequisitionId(key);
            item.setPrice(id.getPrice());
            item.setQuantity(id.getQuantity());


            purchaseDetailBoms.add(item);
        }
        if (collect.isEmpty()) {
            System.out.println("集合为空");
        }
        b = this.saveBatch(purchaseDetailBoms);

        return b;

    }

    @Override
    public  List<PurchaseDetailBom> orderwuliaoId(int id) {
        List<PurchaseDetailBom> orderwuliaoId = purchaseDetailBomMapper.orderwuliaoId(id);
        System.out.println("采购单内容如下" + orderwuliaoId);

        if (orderwuliaoId != null) {
            System.out.println("查询成功");
            return orderwuliaoId;
        }
        return orderwuliaoId;
    }

    public void addmaterials2(List<PurchaseDetailBom2> ids, Integer key) {
        System.err.println("选择物料     = " + ids);
        // 2. 查询order_item中所有已有的物料数据
        List<PurchaseDetailBom2> orderDetailBoms = purchaseDetailBomMapper2.selectList(null);
        //获取 所有 material_id
        Set<Integer> materialIdSet = orderDetailBoms.stream()
                .map(PurchaseDetailBom2::getMaterialId)
                .collect(Collectors.toSet());

        ids.forEach(bom -> {
            if (materialIdSet.contains(bom.getId())) {
                // 存在：获取价格和数量
//                System.out.println("获取物料 " + bom.getMaterialId() +
//                        " 的价格: " + bom.getPrice() +
//                        ", 数量: " + bom.getQuantity());
//                LambdaUpdateWrapper<PurchaseDetailBom2> wrapper = new LambdaUpdateWrapper<>();
//                LambdaQueryWrapper<PurchaseDetailBom2> lambdaQueryWrapper=new LambdaQueryWrapper();
//                lambdaQueryWrapper.eq(PurchaseDetailBom2::getMaterialId,bom.getMaterialId());
//                PurchaseDetailBom2 orderDetailBom = purchaseDetailBomMapper2.selectOne(lambdaQueryWrapper);
//                BigDecimal price = orderDetailBom.getPrice();
//                BigDecimal quantity = orderDetailBom.getQuantity();
////                需要更新的数目
//                BigDecimal totalQuantity = orderDetailBom.getQuantity().add(bom.getQuantity());
//                wrapper.eq(PurchaseDetailBom2::getMaterialId, bom.getMaterialId()).set(PurchaseDetailBom2::getQuantity,totalQuantity);
//                purchaseDetailBomMapper2.update(null,wrapper);
////                修改总价格
//                LambdaQueryWrapper<ErpPurchaseRequestManagement>zongjiage=new LambdaQueryWrapper<>(
//                );
//                zongjiage.eq(ErpPurchaseRequestManagement::getId,bom.getRequisitionId());
//
//                ErpPurchaseRequestManagement erpPurchaseOrderManagement = pur.selectOne(zongjiage);
//                int totalprice = erpPurchaseOrderManagement.getTotalprice();
//                BigDecimal totalAmount = price.multiply(quantity);
//                BigDecimal result = totalAmount.add(new BigDecimal(totalprice));
//                LambdaUpdateWrapper<ErpPurchaseRequestManagement> orderDetailBomLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
//                orderDetailBomLambdaUpdateWrapper.eq(ErpPurchaseRequestManagement::getId,bom.getRequisitionId()).set(ErpPurchaseRequestManagement::getTotalprice,result);
//                pur.update(null,orderDetailBomLambdaUpdateWrapper);
                LambdaUpdateWrapper<PurchaseDetailBom2> queryWrapper = new LambdaUpdateWrapper<>();
               queryWrapper.eq(PurchaseDetailBom2::getMaterialId,bom.getId());
                int rows = purchaseDetailBomMapper2.delete(queryWrapper);
                PurchaseDetailBom2 newItem = new PurchaseDetailBom2();
                System.out.println("新增的物料id = " + bom.getId());
                newItem.setMaterialId(bom.getId());
                newItem.setRequisitionId(key);
                newItem.setPrice(bom.getPrice());
                newItem.setQuantity(bom.getQuantity());
                purchaseDetailBomMapper2.insert(newItem);
            } else {
                // 不存在：新增


                PurchaseDetailBom2 newItem = new PurchaseDetailBom2();
                System.out.println("新增的物料id = " + bom.getId());
                newItem.setMaterialId(bom.getId());
                newItem.setRequisitionId(key);
                newItem.setPrice(bom.getPrice());
                newItem.setQuantity(bom.getQuantity());

                purchaseDetailBomMapper2.insert(newItem);
                System.out.println("新增物料: " + bom.getItemId());

            }
        });
    }
}